﻿#include "TestgRpcPluginImp.h"
#include "TestgRpcServer.h"

#if 0
#include "helloworld.grpc.pb.h"

using helloworld::Greeter;
using helloworld::HelloReply;
using helloworld::HelloRequest;

// Logic and data behind the server's behavior.
class GreeterServiceImpl final : public Greeter::Service {
  Status SayHello(ServerContext* context, const HelloRequest* request,
                  HelloReply* reply) override {
    std::string prefix("Hello ");
    reply->set_message(prefix + request->name());
    return Status::OK;
  }
};
#endif

extern "C" OSGI_EXPORT_SYMBOL void * K5_Bundle_Activator(void)
{
    IBundleActivator* pBund = 0;
    pBund = TestgRpcPluginImp::Instance();
    return (void*)pBund;
}

TestgRpcPluginImp* TestgRpcPluginImp::m_pInstance = NULL;
TestgRpcPluginImp* TestgRpcPluginImp::Instance()
{
    if(m_pInstance == NULL) m_pInstance = new TestgRpcPluginImp();
    return m_pInstance;
}

TestgRpcPluginImp::TestgRpcPluginImp()
{
    m_pBundCnt = 0;
    m_pServMgr = 0;
    m_pAlloc = 0;
    //m_pClusterFactory = 0;
    m_pIniParser = 0;
    m_pLogger = 0;
    //service = 0;
    m_pGRPCService = 0;
}

TestgRpcPluginImp::~TestgRpcPluginImp()
{
    delete m_pBundCnt;
    m_pBundCnt = 0;
    delete m_pServMgr;
    m_pServMgr = 0;
    delete m_pAlloc;
    m_pAlloc =0;
    //delete m_pClusterFactory;
    //m_pClusterFactory = 0;
    delete m_pIniParser;
    m_pIniParser = 0;
    delete m_pLogger;
    m_pLogger = 0;
    //delete service;
    //service = 0;
    delete m_pGRPCService;
    m_pGRPCService = 0;
}

bool TestgRpcPluginImp::setServiceMnager(IBundleContext* pBundleContext)
{
    IServiceManager * pServiceManager = 0;
    int iRet = pBundleContext->GetServiceManager(&pServiceManager);
    if((SUCCESS != iRet) || (0 == pServiceManager))
    {
        m_pServMgr = 0;
        return false;
    }
    m_pServMgr = pServiceManager;
    return true;
}

bool TestgRpcPluginImp::setBundleContext(IBundleContext* pBundleContext)
{
    if(0 == pBundleContext)
        return false;
    m_pBundCnt = pBundleContext;
    return true;
}

bool TestgRpcPluginImp::setAllocator(IBundleContext* pBundleContext)
{
    IAllocator * pAllocator = 0;
    pBundleContext->GetAllocator(&pAllocator);
    if(0 == pAllocator) return false;
    m_pAlloc = pAllocator;
    return true;
}

#if 1
//预留接口，后期如果插件需要配置，可以使用这个接口读取配置
bool TestgRpcPluginImp::setIniConfigParser(IBundleContext* pBundleContext)
{
    IService * pServiceFacConfig = 0;
    m_pServMgr->GetService(IID_ConfigParserFactory, &pServiceFacConfig, SID_ConfigParserFactory, 0);
    if(0 == pServiceFacConfig)
        return false;
    IConfigParserFactory * pConfigParserFactory = (IConfigParserFactory*)pServiceFacConfig;
    //从配置文件中获取基础配置
    m_pIniParser = (IINIConfigParser*)pConfigParserFactory->CreateConfigParser("TestgRpcService.ini","TestgRpcServicePlugin",INI);
    if(0 == m_pIniParser)
        return false;

    const char * pValue = 0;

    //获取gRpc服务端配置ip
    if(false == m_pIniParser->GetValueOrDefault("Server","serverIP",&pValue,"0.0.0.0")) return false;
    server_address = pValue;
    printf("获得gRpc服务端配置ini文件中%s节点下%s参数的信息为: %s .\n","Server","serverIP",server_address.c_str());

    //获取gRpc服务端配置port
    if(false == m_pIniParser->GetValueOrDefault("Server","serverPort",&pValue,"50051")) return false;
    server_address = server_address + ":" + pValue;
    int ServerPort = atoi(pValue);
    printf("获得gRpc服务端配置ini文件中%s节点下%s参数的信息为: %d .\n","Server","serverPort",ServerPort);
    printf("gRpc服务端配置IP端口信息为: %s .\n",server_address.c_str());
    if(true == m_pIniParser->GetValueOrDefault("Server","enablepoll",&pValue,"0")){
        //printf("1111111\n");
        if(1 == atoi(pValue)){
            enpollflag = true;
            printf("当前gRpc服务端配置已启用线程池\n");
            if(false == m_pIniParser->GetValueOrDefault("Server","minnum",&pValue,"4")){
                printf("当前gRpc服务端未配置最小线程数，默认值为4\n");
                minnum = 4;
            }else{
                minnum = atoi(pValue);
            }
            if(false == m_pIniParser->GetValueOrDefault("Server","maxnum",&pValue,"8")){
                printf("当前gRpc服务端未配置最大线程数，默认值为8\n");
                maxnum = 8;
            }else{
                maxnum = atoi(pValue);
            }
            printf("当前gRpc服务端配置最小线程数为%d,最大线程数为%d\n",minnum,maxnum);
        }
    }else{
        //printf("5555555!!!!!!!\n");
        enpollflag = false;
        minnum = 0;
        maxnum = 0;
        printf("当前gRpc服务端配置未启用线程池\n");
    }

    printf("获取gRpc服务端配置ini信息完成\n");

    return true;
}
#endif

bool TestgRpcPluginImp::setLogger(IBundleContext* pBundleContext)
{
    IService * pLogFac = 0;
    m_pServMgr->GetService(IID_LoggerFactory,&pLogFac,SID_LoggerFactory,0);
    if(0 == pLogFac)
        return false;
    ILoggerFactory *pLoggerFactory = (ILoggerFactory *)pLogFac;

    m_pLogger = pLoggerFactory->CreateBundleLogger("TestgRpcService.log","TestgRpcServicePlugin");
    if(m_pLogger == NULL) return false;
    return true;
}

/*
bool TestgRpcPluginImp::setClusterManagerFac(IBundleContext* pBundleContext)
{
    IService * pServiceFacCluster = 0;
    m_pServMgr->GetService(IID_ClusterFactory, &pServiceFacCluster, SID_ClusterFactory, 0);
    if(0 == pServiceFacCluster)
        return false;
    m_pClusterFactory = (IClusterFactory*)pServiceFacCluster;
    return true;
}
*/

bool TestgRpcPluginImp::setGRPCService(IBundleContext* pBundleContext)
{
    //printf("6666666[%s]\n",__func__);
    IService * pGRPCService = 0;
    m_pServMgr->GetService(IID_GRPC_Server,&pGRPCService,SID_GRPC_Server,0);
    if(0 == pGRPCService)
        return false;
    //printf("777777777[%s]\n",__func__);
    m_pGRPCService = (IGRPCService *)pGRPCService;
    //printf("8888888888[%s]\n",__func__);
    //m_pGRPCService->enablepoll(enpollflag);
    //m_pGRPCService->setminpollnum(minnum);
    //m_pGRPCService->setmaxpollnum(maxnum);
    printf("enpollflag[%d],minnum[%d],maxnum[%d]\n", enpollflag, minnum, maxnum);
    //printf("m_pGRPCService->m_threadpoll[%d],m_pGRPCService->min_poll[%d],m_pGRPCService->max_poll[%d]\n", m_pGRPCService->m_threadpoll, m_pGRPCService->min_poll, m_pGRPCService->max_poll);
    return true;
#if 0
    //处理业务  目前测试的例子都在封装在调用的用例中，没有在框架中直接插入测试用例。
    service = (GreeterServiceImpl *) service;
    //GreeterServiceImpl service;
    m_pGRPCService->gRPCSynServerInit(server_address);
    m_pGRPCService->gRPCSynServerRegister(service);
    m_pGRPCService->gRPCSynServerstart();
#endif
}

ILogger * TestgRpcPluginImp::GetLogger()
{
    return m_pLogger;
}

IGRPCService * TestgRpcPluginImp::GetGRPCService()
{
    return m_pGRPCService;

}

bool MKRAPI TestgRpcPluginImp::Start(IBundleContext * pBundleContext)
{

    bool bRet = false;
    try
    {
        //初始化
        if(setBundleContext(pBundleContext) != true)
            return bRet;
        if(setAllocator(pBundleContext) != true)
            return bRet;
        if(setServiceMnager(pBundleContext) != true)
            return bRet;
        if(setIniConfigParser(pBundleContext) != true)
            return bRet;
        if(setLogger(pBundleContext) != true)
            return bRet;
        //printf("555555555\n");
        if(setGRPCService(pBundleContext) != true)
        {
            //printf("1111111---%d\n",bRet);
            return bRet;
        }


        //printf("1111111\n");
        //readConfig(server_address);

        TestGRPCServer * pTestgRpcService = TestGRPCServer::Instance();
        //printf("22222222\n");
#if 1

        //同步测试用例的调用,同步例子的异步例子只启用一种，暂时目前测试例子中配置文件是使用的同一个
        //(char *server_address,bool enablepoll,int min_poll,int max_poll)
        pTestgRpcService->TestgRPCSynServerRun(m_pGRPCService, server_address, enpollflag, minnum, maxnum);
#else
        //异步测试用例的调用，需要调用者自己处理回调信息，比如下边的例子
        //pTestgRpcService->TestgRPCAsynServerRun(m_pGRPCService, server_address,server.cq_, server.server_, &(server.service_));
        pTestgRpcService->TestgRPCAsynServerRun(m_pGRPCService, server_address);
#endif
        //printf("333333333\n");
        IService * pTestgRpcServer = 0;
        //TestGRPCServer * pTestgRpcService = TestGRPCServer::Instance();
        pTestgRpcServer = (TestGRPCServer *)pTestgRpcService;
        m_pServMgr->RegisterService(IID_GRPC_TestServer,pTestgRpcServer,SID_GRPC_TestServer,NULL);

        //printf("4444444444\n");

        bRet = true;

    }
    catch(...)
    {
        bRet = false;
    }

    return bRet;
}

bool MKRAPI TestgRpcPluginImp::Stop(IBundleContext * pBundleContext)
{
    return true;
}

bool TestgRpcPluginImp::readConfig(std::string server_address)
{

    IService * pServiceFacConfig = 0;
    m_pServMgr->GetService(IID_ConfigParserFactory, &pServiceFacConfig, SID_ConfigParserFactory, 0);
    if(0 == pServiceFacConfig)
        return false;

    IConfigParserFactory * pConfigParserFactory = (IConfigParserFactory*)pServiceFacConfig;

    //从配置文件中获取基础配置
    IINIConfigParser * pIniConfigParser = (IINIConfigParser *)pConfigParserFactory->CreateConfigParser("TestgRpcService.ini","TestgRpcServicePlugin",INI);
    if(0 == pIniConfigParser) return false;


    const char * pValue = 0;

    //获取gRpc服务端配置ip
    if(false == pIniConfigParser->GetValueOrDefault("Server","serverIP",&pValue,"0.0.0.0")) return false;
    server_address = pValue;
    printf("获得gRpc服务端配置ini文件中%s节点下%s参数的信息为: %s .\n","Server","serverIP",server_address.c_str());

    //获取gRpc服务端配置port
    if(false == pIniConfigParser->GetValueOrDefault("Server","serverPort",&pValue,"50051")) return false;
    server_address = server_address + ":" + pValue;
    int ServerPort = atoi(pValue);
    printf("获得gRpc服务端配置ini文件中%s节点下%s参数的信息为: %d .\n","Server","serverPort",ServerPort);
    printf("gRpc服务端配置IP端口信息为: %s .\n",server_address.c_str());
    printf("获取gRpc服务端配置ini信息完成\n");
    return true;
}
#if 0
void gRpcPluginImp::registerToCluster(std::string &strServiceName,ServerServiceName &info)
{
    ICluster * serviceCluster = m_pClusterFactory->CreateCluster(strServiceName.c_str());
    SERVICE_BASEINFO baseInfo;
    baseInfo.pAddr = info.ip;
    baseInfo.port = info.port;
    baseInfo.protocol = 0;//wei ding yi
    IDictionary * instanceInfo = (IDictionary *)CreateDictionary();;
    char * instanceID = 0;

    serviceCluster->RegServiceInstance(&baseInfo,instanceInfo,&instanceID);5

}
#endif


